home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / code / chap18 / Wr.java < prev   
Encoding:
Java Source  |  1997-04-20  |  1.1 KB  |  39 lines

  1. import java.io.*;
  2. import java.net.URL;
  3.  
  4. public class Wr {
  5.    private static final String FILE = "example";
  6.    public static void main(String[] args) {
  7.       write();
  8.       read();
  9.    }
  10.  
  11.    private static void write() {
  12.       try {
  13.          FileOutputStream f = new FileOutputStream(FILE);
  14.          ObjectOutput s = new ObjectOutputStream(f);
  15.          s.writeObject("My home page is: ");
  16.          s.writeObject(new URL("http://www.learnjava.com"));
  17.          s.close();
  18.       } catch (IOException x) {
  19.          System.out.println(x.getMessage());
  20.       }
  21.    }
  22.  
  23.    private static void read() {
  24.       try {
  25.          FileInputStream f = new FileInputStream(FILE);
  26.          ObjectInput s = new ObjectInputStream(f);
  27.          String text = (String)(s.readObject());
  28.          URL url = (URL)(s.readObject());
  29.          System.out.println(text + url);
  30.          s.close();
  31.       } catch (IOException x) {
  32.          System.out.println(x.getMessage());
  33.       } catch (ClassNotFoundException x) {
  34.          System.out.println(x.getMessage());
  35.       }
  36.    }
  37.  
  38. }
  39.